<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			input{
				padding-left:10px;
				width:100px;
			}
			.date{
				display: flex;
			}
			span{
				margin:0 10px;
			}
		</style>
	</head>
	<body>
		<div class="date">
			<div class="line1"><input type="text" id="test1" placeholder="请选择开始时间"></div><span>-</span>
			<div class="line2"><input type="text" id="test2" placeholder="请选择开始时间"></div>
		</div>
	</body>

	<script src="laydate/laydate.js"></script>
	<script src="js/jquery.min.js"></script>
	<script>
		var myDate = new Date();
		let year = myDate.getFullYear();
		let month = myDate.getMonth() >= 10 ? myDate.getMonth() : '0' + myDate.getMonth();
		// let month=myDate.getMonth();
		if (month == 0) {
			var ym2 = (year - 1) + ' 年 ' + (12) + ' 月';
		} else {
			var ym2 = year + ' 年 ' + (month) + ' 月';
		}
		var mon = (12 - (12 - month) + 1) >= 10 ? (12 - (12 - month) + 1) : '0' + (12 - (12 - month) + 1);
		let ym1 = year - 1 + ' 年 ' + mon + ' 月';

		laydate.render({
			elem: '#test2',
			type: 'month',
			value: ym2,
			format: 'yyyy 年 MM 月',
			max: new Date().getFullYear() + '-' + (new Date().getMonth()) + '-' + new Date().getDate(),
			btns: ['clear', 'confirm'],

			done: function(value, date) {

				let years = date.year;
				let months = date.month;

				var maxStr = years + '-' + months + '-31';
				if (months == 12) {

					var minStr = (years) + '-' + '01-01';
				} else {
					var mon = (12 - (12 - months) + 1) >= 10 ? (12 - (12 - months) + 1) : '0' + (12 - (12 - months) + 1);

					var minStr = (years - 1) + '-' + (mon) + '-01';
				}

				$('#test1').remove();
				$('.line1').html('<input type="text" id="test1" placeholder="请选择开始时间">');
				laydate.render({
					elem: '#test1',
					type: 'month',
					// value:str,
					format: 'yyyy 年 MM 月',
					max: maxStr,
					min: minStr,
					btns: ['clear', 'confirm'],
				});
			},
		});
		laydate.render({
			elem: '#test1',
			type: 'month',
			value: ym1,
			format: 'yyyy 年 MM 月',
			max: new Date().getFullYear() + '-' + new Date().getMonth() + '-' + new Date().getDate(),
			btns: ['clear', 'confirm'],
			done: function(value, date) {

				let years = date.year;
				let months = date.month;

				var Mix = years + '-' + (months) + '-01';
				if (months == 1) {
					var y = years;
					var m = 12;
					var Max = y + '-' + m + '-31';
				} else {
					var y = years + 1;
					var m = (12 - (12 - months) - 1) >= 10 ? (12 - (12 - months) - 1) : '0' + (12 - (12 - months) - 1);
					var Max = y + '-' + m + '-31';
				}

				if (y > year) {
					var MaxF = new Date().getFullYear() + '-' + (new Date().getMonth()) + '-' + new Date().getDate();
				} else if ((m > month) && (y == year)) {
					var MaxF = new Date().getFullYear() + '-' + (new Date().getMonth()) + '-' + new Date().getDate();
				} else {
					var MaxF = Max;
				}
				console.log(MaxF + '===' + Mix);
				$('#test2').remove();
				$('.line2').html('<input type="text" id="test2" placeholder="请选择开始时间">');
				laydate.render({
					elem: '#test2',
					type: 'month',
					// value:str,
					format: 'yyyy 年 MM 月',
					max: MaxF,
					min: Mix,
					btns: ['clear', 'confirm'],
				});
			}
		});
	</script>
</html>
